'use strict';
import { Alert, AsyncStorage, Dimensions, NativeModules, PixelRatio, Platform } from 'react-native';

import _ from './underscore';
import dd from './iotauth';
const BASE_URL = "https://api.bestwond.com/";

//是否按设备地址
var onTab = false;
var whoosh;
var uiWidth = 1280;
var uiHeight = 800;
var uiWidth_can = 1280;
var uiHeight_can = 800;
var pixel = 1 / PixelRatio.get();
var screenWidth = Dimensions.get('window').width;
var screenHeight = Dimensions.get('window').height;
var pixelRatio = PixelRatio.get();
var fontScale = PixelRatio.getFontScale();
var scale = Math.min(
    Dimensions.get('window').height / uiHeight,
    Dimensions.get('window').width / uiWidth,
);
var scale_can = Math.min(
    Dimensions.get('window').height / uiHeight_can,
    Dimensions.get('window').width / uiWidth_can,
);
var utils = {
   
    getURL() {
        return BASE_URL
      
    },
   async xhrsign(url, params) {
        try {
            let app_id ="*********";//appid
            if (params) {
                params.app_id =app_id
            } else {
                var params = {};
            }
           
            let app_secret = "********";//密钥
            let sign = this.create_sign(params,app_secret)
            return new Promise((resolve, reject) => {
                fetch(url+"?sign="+sign, {
                    method: 'POST', //定义请求方式，POST、GET、PUT等
                    headers: {
                        'Accept': 'application/json', // 提交参数的数据方式,这里以json的形式
                        'Content-Type': 'application/json',
                        "Connection": "keep-Alive"
                    },
                    body: JSON.stringify(params), //提交的参数3
                })
                    .then(response => response.json()) //数据解析的方式，json解析
                    .then(response => {
                        // var res = responseJson.code; //返回直接映射完的数据，可以直接使用
                        resolve(response);
                        // alert(JSON.stringify(responseJson))
                    }).catch((error) => {
                        console.log(error);
                        reject(error)
                       
                    });
            });
        } catch (e) {
            
        }
    },
    
    create_sign(data, bw_key) {
        // data.timestamps=new Date().getTime();
        var keys = _.sortBy(_.keys(data));
        var d = '';
        var that = this;
    
        console.log('dev_key=', bw_key);
        _.map(keys, function (key) {
          if (key == _.last(keys)) {
            // + new Date().getTime()
            d += key + '=' + that.url_encode(data[key]) + bw_key;
          } else {
            d += key + '=' + that.url_encode(data[key]) + '&';
          }
        });
        console.log(d,"aaaaaaaaaa");
      
        console.log(d,"ppppppppppppppppppp");
        var sign = dd.hex_sha512(d);
        return sign;
      },
      url_encode(str) {
        str = (str + '').toString();
        return (
          encodeURIComponent(str)
          .replace(/\[/g,'%5B')
          .replace(/!/g, '%21')
          .replace(/'/g, '%27')
          .replace(/\(/g, '%28')
          .replace(/\)/g, '%29')
          //.replace(/\*/g, '%2A')
          //.replace(/\+/g, '%20')
          .replace(/\]/g,'%5D')
        );
      },
 
};

module.exports = utils;
